<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="社区名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入社区名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开发商" prop="developers">
        <el-input
          v-model="queryParams.developers"
          placeholder="请输入开发商"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物业公司" prop="propertyCompany">
        <el-input
          v-model="queryParams.propertyCompany"
          placeholder="请输入物业公司"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入市"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="县、区" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入县、区"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详细地址" prop="addr">
        <el-input
          v-model="queryParams.addr"
          placeholder="请输入详细地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:community:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:community:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:community:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:community:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="communityList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form label-width="100px">
            <el-form-item label="开发商">
              {{ scope.row.developers }}
            </el-form-item>
            <el-form-item label="物业公司">
              {{ scope.row.propertyCompany }}
            </el-form-item>
            <el-form-item label="地区">
              {{ scope.row.province }} {{ scope.row.city }} {{ scope.row.area }}
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column type="index" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="创建人" align="center" prop="userName" />
      <el-table-column label="占地面积" align="center">
        <template slot-scope="scope">
          {{ scope.row.floorSpace }} m<sup>2</sup>
        </template>
      </el-table-column>
      <el-table-column label="建筑面积" align="center" prop="coveredArea">
        <template slot-scope="scope">
          {{ scope.row.coveredArea }} m<sup>2</sup>
        </template>
      </el-table-column>
      <el-table-column label="详细地址" align="center" prop="addr" />
      <el-table-column label="操作" align="center" width="200px">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:community:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['system:community:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            v-hasPermi="['system:community:remove']"
            size="mini"
            type="text"
            icon="el-icon-arrow-right"
            @click="changeCommunity(scope.row)"
          >切换</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改社区对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="userId">
          <el-input v-model="form.name" placeholder="请输入社区的名称" />
        </el-form-item>
        <el-form-item label="占地面积" prop="floorSpace">
          <el-input v-model="form.floorSpace" placeholder="请输入占地面积" />
        </el-form-item>
        <el-form-item label="建筑面积" prop="coveredArea">
          <el-input v-model="form.coveredArea" placeholder="请输入建筑面积" />
        </el-form-item>
        <el-form-item label="开发商" prop="developers">
          <el-input v-model="form.developers" placeholder="请输入开发商" />
        </el-form-item>
        <el-form-item label="物业公司" prop="propertyCompany">
          <el-input v-model="form.propertyCompany" placeholder="请输入物业公司" />
        </el-form-item>
        <el-form-item label="地区" prop="province">
          <v-distpicker :province="form.province" :city="form.city" :area="form.area" @selected="selectedArea" />
        </el-form-item>
        <el-form-item label="详细地址" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入详细地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCommunity, getCommunity, delCommunity, addCommunity, updateCommunity, exportCommunity } from '@/api/system/community'
import { changeUserCommunityId } from '../../../api/system/user'

export default {
  name: 'Community',
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 社区表格数据
      communityList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        developers: null,
        propertyCompany: null,
        province: null,
        city: null,
        area: null,
        addr: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: '社区名称不能为空', trigger: 'blur' }
        ],
        province: [
          { required: true, message: '省不能为空', trigger: 'blur' }
        ],
        city: [
          { required: true, message: '市不能为空', trigger: 'blur' }
        ],
        area: [
          { required: true, message: '县、区不能为空', trigger: 'blur' }
        ],
        addr: [
          { required: true, message: '详细地址不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 切换社区 **/
    changeCommunity(community) {
      this.$confirm('是否切换为【' + community.name + '】小区', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 修改用户中的当前社区id
        changeUserCommunityId(community.communityId).then(response => {
          this.$message('请重新登录')
          // 修改成功后退出
          this.$store.dispatch('LogOut').then(() => {
            location.href = '/index'
          })
        })
      })
    },
    /** 查询社区列表 */
    getList() {
      this.loading = true
      listCommunity(this.queryParams).then(response => {
        this.communityList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        communityId: null,
        name: null,
        userId: null,
        floorSpace: null,
        coveredArea: null,
        developers: null,
        propertyCompany: null,
        createTime: null,
        province: null,
        city: null,
        area: null,
        addr: null,
        delFlag: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.communityId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加社区'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.communityId || this.ids
      getCommunity(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改社区'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.communityId != null) {
            updateCommunity(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCommunity(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.communityId || this.ids
      this.$confirm('是否确认删除社区编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delCommunity(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有社区数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportCommunity(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    },
    // 选择省市区触发事件
    selectedArea(data) {
      this.form.province = data.province.value
      this.form.city = data.city.value
      this.form.area = data.area.value
    }
  }
}
</script>
